/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.starter.web.config;

import cn.hallele.ms.infrastructure.plugin.captcha.CaptchaClient;
import cn.hallele.ms.infrastructure.plugin.safety.encoder.PasswordEncoder;
import cn.hallele.ms.starter.web.config.proereties.PluginProperties;
import cn.hallele.ms.starter.web.encoder.RSAPasswordEncoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * hallele_mc_core
 *
 * @author anle5
 * @since 2024/9/15 13:55
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(PluginProperties.class)
public class PluginConfig {
    /**
     * 用户登录密码RSA加解密插件
     *
     * @param properties 配置
     * @return PasswordEncoder
     */
    @Bean
    @ConditionalOnProperty(value = "hal.ms.infrastructure.plugin.safety.enabled", havingValue = "true")
    public PasswordEncoder passwordEncoder(PluginProperties properties) {
        PluginProperties.SafetyProperties safety = properties.getSafety();
        return new RSAPasswordEncoder(safety.getLoginPassPriKey(), safety.getLoginPassPubKey());
    }

    /**
     * 验证码插件
     *
     * @param properties 配置
     * @return Captcha
     */
    @Bean
    @ConditionalOnProperty(value = "hal.ms.infrastructure.plugin.captcha.enabled", havingValue = "true")
    public CaptchaClient captcha(PluginProperties properties) {
        PluginProperties.CaptchaProperties captcha = properties.getCaptcha();
        return new CaptchaClient(captcha.getCodeCount(),
                captcha.getHeight(),
                captcha.getInterfereCount(),
                captcha.getTtl(),
                captcha.getWidth());
    }
}
